package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


// 收货地址
@Service
@Transactional
public class AddressServiceImpl implements AddressService {

    //依赖注入mapper层
    @Autowired
    private AddressMapper addressMapper;

    //查询地址列表
    @Override
    public List<Address> findList() {
        //业务处理
        //调用mp根据UserId查询当前用户存储的地址列表
        return addressMapper.selectList(new LambdaQueryWrapper<Address>().eq(Address::getUserId, UserHolder.get().getId()));
    }

    //新增收货地址
    @Override
    public void save(Address address) {
        //业务处理
        //1.生成ID(雪花算法)
        address.setId(IdUtil.getSnowflake(1,1).nextId());
        //2.关联当前用户id
        address.setUserId(UserHolder.get().getId());
        //3.调用mp新增
        addressMapper.insert(address);
    }

    //修改(设置)默认收货地址
    @Override
    public void updateById(Long id) {
        //业务处理
        Address address = new Address();
        //1.先根据UserId将所有默认收货地址设为0
        //1-1将默认值设置为0
        address.setIsDefault(0);
        //1-2调用mp根据UserId更新
        addressMapper.update(address,new LambdaQueryWrapper<Address>().eq(Address::getUserId,UserHolder.get().getId()));

        //2.在通过传入的参数将修改指定id的默认收货地址
        //2-1将默认值设为1
        address.setIsDefault(1);
        //2-2将id设置为传入的参数
        address.setId(id);
        //3. 调用mp根据id更新
        addressMapper.updateById(address);
        //addressMapper.update(address,new LambdaQueryWrapper<Address>().eq(Address::getId,id));
    }

    //(去结算)获取默认收货地址
    @Override
    public Address findDefaultAddress() {
        //业务处理
        //根据根据default为1调用mp查询
        return addressMapper.selectOne(new LambdaQueryWrapper<Address>().eq(Address::getIsDefault,1).eq(Address::getUserId,UserHolder.get().getId()));
    }

    //回显收货地址
    @Override
    public Address findById(Long id) {
        //业务处理
        //1.根据id查询address表
        return addressMapper.selectById(id);
    }

    //修改收货地址
    @Override
    public void updateByAddress(Address address) {
        //业务处理
        //调用mp修改收货地址
        addressMapper.updateById(address);
    }

    //(批量)删除收货地址
    @Override
    public void deleteById(List<Long> ids) {
        //业务处理
        //1.使用对象前先判空
        if (CollectionUtil.isEmpty(ids)){
            throw new CustomException("删除失败:此对象已被删除...");
        }
        //2.调用mp进行删除
        addressMapper.deleteBatchIds(ids);
    }
}
